﻿''
''Copyright Zac McKenzie 2012
''

Imports System.Linq

Public Class scheduledEvent

    Public progName As String
    Public startTime As DateTime
    Public stopTime As DateTime
    Public progLength As Integer
    Public channel As String
    Public repeat As String
    Public active As Boolean
    Public rating As String
    Public starRating As String
    Public category1 As String
    Public category2 As String
    Public category3 As String
    Public category4 As String
    Public category5 As String
    Public desc As String
    Public svcId As String

    Public issueText As String
    Public issueCategory As String

    Public ozTivoEquivalent As scheduledEvent
    Public tsId As String
    Public onId As String

    Dim NextMjd As String
    Dim iceCid As String
    Dim iceUidHi As String
    Dim iceUidLo As String
    Dim play As String
    Dim lock As String
    Dim delete As String
    Dim ttl As String
    Dim fname As String
    Dim StartMjd As String

    Dim start As String
    Public duration As String

    'this one is used from ozTivo list
    Public Sub New(ByVal _progName As String, ByVal _startTime As DateTime, ByVal _stopTime As DateTime, ByVal _progLength As Integer, ByVal _duration As Integer, ByVal _channel As String, ByVal _repeat As String, ByVal _rating As String, ByVal _starRating As String, ByVal _category1 As String, ByVal _category2 As String, ByVal _category3 As String, ByVal _category4 As String, ByVal _category5 As String, ByVal _desc As String)
        progName = _progName
        startTime = _startTime
        stopTime = _stopTime
        progLength = _progLength
        duration = _duration
        channel = _channel
        repeat = _repeat
        rating = _rating
        starRating = _starRating
        category1 = _category1
        category2 = _category2
        category3 = _category3
        category4 = _category4
        category5 = _category5
        desc = _desc

        Dim s As svc = (From xxxx In config.channelList Where xxxx.Value.channelName = channel Select xxxx.Value).FirstOrDefault

        svcId = s.channelId
        onId = s.onId
        tsId = s.tsId

        iceUidLo = (From x As scheduledEvent In bwAccessor.timerList Order By startTime Descending Select x.iceUidLo).FirstOrDefault

    End Sub

    'This one is used from  beyonWiz timers
    Public Sub New(ByVal t As System.Xml.Linq.XElement)
        issueText = ""
        NextMjd = t.@nextMjd
        iceCid = t.@iceCid
        iceUidHi = t.@iceUidHi
        iceUidLo = t.@iceUidLo
        play = t.@play
        lock = t.@lock
        tsId = t.@tsId
        onId = t.@onId
        delete = t.@delete
        ttl = t.@ttl
        fname = t.@fname
        StartMjd = t.@startMjd
        svcId = t.@svcId
        start = t.@start
        repeat = t.@repeat
        duration = t.@duration

        If t.Name = "Timer" Then
            progName = t.@fname

            Dim sh As Integer
            Dim sm As Integer
            Integer.TryParse(t.@start, sh)

            sm = sh
            sh = CInt(System.Math.Truncate(sh / 3600))
            sm = CInt((sm - (sh * 3600)) / 60)
            Dim nextModifiedJulianDate As Integer
            Integer.TryParse(t.@nextMjd, nextModifiedJulianDate)
            startTime = New DateTime(2010, 1, 1, sh, sm, 0).AddDays(nextModifiedJulianDate - 55197)

            channel = channelTranslate(t.@svcId).channelName

            Dim d As Integer
            Integer.TryParse(t.@duration, d)
            progLength = d / 60

            stopTime = startTime.AddMinutes(progLength)
        End If
    End Sub
    Public Function channelTranslate(ByVal svcId As String) As svc

        Dim svcDetail As svc
        Try
            svcDetail = config.channelList.Item(svcId)
            Return svcDetail
        Catch ex As KeyNotFoundException
            issueText = "Unknown Channel"
            Return New svc(svcId, "Unknown", Nothing, Nothing)
        End Try

        Return (svcDetail)
    End Function

    Public Function clone() As scheduledEvent
        'Dim _orig = (From x In bwAccessor.timerList Order By startTime Descending Select x).FirstOrDefault

        'Dim _clone As scheduledEvent = New scheduledEvent(Me.progName, Me.startTime, Me.stopTime, Me.progLength, Me.duration, Me.channel, Me.repeat, Me.rating, Me.starRating, Me.category1, Me.category2, Me.category3, Me.category4, Me.category5, Me.desc)

        Dim _clone As scheduledEvent = New scheduledEvent(Me.progName, Me.startTime, Me.stopTime, Me.progLength, Me.duration, Me.channel, Me.repeat, Me.rating, Me.starRating, Me.category1, Me.category2, Me.category3, Me.category4, Me.category5, Me.desc)


        _clone.issueText = Me.issueText
        _clone.issueCategory = Me.issueCategory
        _clone.ozTivoEquivalent = Me
        Return _clone
    End Function

    Public Overrides Function toString() As String
        If start = "" Then
            Dim mjds As Double = startTime.Subtract(DateTime.Parse("1858-11-17")).TotalDays 'with decimals
            Dim mjd As Integer = Math.Truncate(mjds)
            Dim secs As Integer = (mjds - mjd) * 84600 'part of the day
            StartMjd = mjd
            NextMjd = mjd
            start = startTime.TimeOfDay.TotalSeconds
            duration = stopTime.Subtract(startTime).TotalSeconds
            'Dim c As String = ozTivoListing.config.channelList2.Item(svcId)

            repeat = "0"
            play = "0"
            lock = "0"
            delete = "0"
            ttl = "0"
        End If
        Dim x = "        <Timer  fname=""" & progName &
                """ iceUidHi=""-1"" iceUidLo=""" & iceUidLo &
                """ iceCid=""-1"" startMjd=""" & StartMjd &
                """ nextMjd=""" & NextMjd &
                """ start=""" & start &
                """ duration=""" & duration &
                """ repeat=""" & repeat &
                """ play=""" & play &
                """ lock=""" & lock &
                """ onId=""" & onId &
                """ tsId=""" & tsId &
                """ svcId=""" & svcId &
                """ delete=""" & delete &
                """ ttl=""" & ttl & """ />"
        Return x

    End Function
    Public Function outputShortStrings() As String()
        If start = "" Then
            Dim mjds As Double = startTime.Subtract(DateTime.Parse("1858-11-17")).TotalDays 'with decimals
            Dim mjd As Integer = Math.Truncate(mjds)
            Dim secs As Integer = (mjds - mjd) * 84600 'fraction of day
            StartMjd = mjd
            NextMjd = mjd
            start = startTime.TimeOfDay.TotalSeconds
            duration = stopTime.Subtract(startTime).TotalSeconds
            'Dim c As String = ozTivoListing.config.channelList2.Item(svcId)

            repeat = "0"
            play = "0"
            lock = "0"
            delete = "0"
            ttl = "0"
        End If
        Dim p As String
        If progName.Length > 200 Then p = progName.Substring(200) Else p = progName

        Dim x = "        <Timer  fname=""" & p & """"
        Dim y = "                iceUidHi=""-1"" iceUidLo=""" & iceUidLo &
                """ iceCid=""-1"" startMjd=""" & StartMjd &
                """ nextMjd=""" & NextMjd &
                """ start=""" & start &
                """ duration=""" & duration &
                """ repeat=""" & repeat &
                """ play=""" & play &
                """ lock=""" & lock &
                """ onId=""" & onId &
                """ tsId=""" & tsId &
                """ svcId=""" & svcId &
                """ delete=""" & delete &
                """ ttl=""" & ttl & """ />"
        Dim sA(2) As String
        sA(0) = x
        sA(1) = y

        Return sA

    End Function

    Public Function parmArray() As String()
        Dim pa(20) As String
        pa(0) = "R"
        pa(1) = issueText
        pa(2) = progName
        pa(3) = startTime.ToShortDateString & "  " & startTime.ToShortTimeString
        pa(4) = stopTime.ToShortDateString & "  " & stopTime.ToShortTimeString
        pa(5) = progLength.ToString
        pa(6) = channel
        pa(7) = repeat.ToString
        pa(8) = active.ToString
        pa(9) = rating
        pa(10) = starRating
        pa(11) = category1
        pa(12) = category2
        pa(13) = category3
        pa(14) = category4
        pa(15) = category5
        pa(16) = desc
        pa(17) = svcId
        pa(18) = onId
        pa(19) = tsId
        Return pa

    End Function
    Public Function issueSideEffect(ByVal fixPart As String, ByVal variablePart As String, ByVal bwTimer As scheduledEvent, ByVal issueCat As String, Optional ByVal ozTivoSchedule As scheduledEvent = Nothing) As String
        Debug.Assert((issueCat = "W" And Not ozTivoSchedule Is Nothing) Or issueCat <> "W")
        Me.issueCategory = issueCat
        Me.issueText = fixPart.PadRight(25, " ") & variablePart
        If bwTimer IsNot Nothing And ozTivoSchedule IsNot Nothing Then bwTimer.ozTivoEquivalent = ozTivoSchedule
        Return Me.issueText
    End Function
End Class
